package com.zixue.heima.nio;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : ServerDemo
 * @Description : TODO
 * @Author : chunxiong
 * @Date : 2025/4/15 21:57
 * @Version 1.0
 */
@Slf4j
public class ServerNioDemo {
    public static void main(String[] args) throws IOException {

        ByteBuffer  byteBuffer = ByteBuffer.allocate(10);
        //1。创建服务器
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        //2。绑定端口
        serverSocketChannel.bind(new InetSocketAddress(8080));
        serverSocketChannel.configureBlocking(false);
        //3.建立链接
//        serverSocketChannel.accept();
        //希望端口能被多次调用
        List<SocketChannel>  list = new ArrayList<>();
        while (true){
            //4. accept 建立与客户端链接    SocketChannel用来与客户端通信
//            log.debug("accept之前。。。。");
            SocketChannel socketChannel = serverSocketChannel.accept();
            //每次建立的链接放到集合里面去
            if (socketChannel != null){
                log.debug("accept之后。。。。{}", socketChannel);
//                socketChannel.configureBlocking(false);
                list.add(socketChannel);
            }
            //客户端要与服务端通信， 就要便利这些集合
            for (SocketChannel channel : list) {
                //5。接受 客户端发来的数据
                log.debug("read之前。。。。{}",channel);
                channel.read(byteBuffer);
                byteBuffer.flip();  //切换读模式
                CharBuffer decode = Charset.defaultCharset().decode(byteBuffer);
                System.out.println(decode);
                System.out.println(byteBuffer);
                byteBuffer.clear();//切换写模式
                log.debug("read之后。。。。{}",channel);
            }
            //问题这种非阻塞模式 虽然可以处理多个客户端连接 ，但是一直循环 劳碌死，
        }
    }

    //一直打印不阻塞accept
    public static void main2() throws IOException {
        ByteBuffer  byteBuffer = ByteBuffer.allocate(10);
        //1。创建服务器
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        //2。绑定端口
        serverSocketChannel.bind(new InetSocketAddress(8080));
        serverSocketChannel.configureBlocking(false);
        //3.建立链接
//        serverSocketChannel.accept();
        //希望端口能被多次调用
        List<SocketChannel>  list = new ArrayList<>();
        while (true){
            //4. accept 建立与客户端链接    SocketChannel用来与客户端通信
            log.debug("accept之前。。。。");
            SocketChannel socketChannel = serverSocketChannel.accept();
            log.debug("accept之后。。。。{}", socketChannel);
            //每次建立的链接放到集合里面去
//            list.add(socketChannel);
//            //客户端要与服务端通信， 就要便利这些集合
//            for (SocketChannel channel : list) {
//                //5。接受 客户端发来的数据
//                log.debug("read之前。。。。{}",channel);
//                channel.read(byteBuffer);
//                byteBuffer.flip();  //切换读模式
//                CharBuffer decode = Charset.defaultCharset().decode(byteBuffer);
//                System.out.println(decode);
//                System.out.println(byteBuffer);
//                byteBuffer.clear();//切换写模式
//                log.debug("read之后。。。。{}",channel);
//            }
        }
    }
}
